import weakref


class ExpensiveObject:

    def __del__(self):
        print('(Deleting {}'.format(self))


def callback(reference):
    """Invoking when referenced object is deleted"""
    print('callback({!r}'.format(reference))


obj = ExpensiveObject()
ref = weakref.ref(obj, callback)

print('obj: ', obj)
print('ref: ', ref)
print('r(): ', ref())

print('deleting obj')
del obj
print('r(): ', ref())
